<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 *
 */
class Namecheap extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Dnsrecord;
    }
    /**
     * 导入namecheap活跃域名
     */
    public function namecheap()
    {
        //暂定url
        $url = "http://api.namecheap.com/xml.response?ApiUser=waitingnow&ApiKey=6e42d68999274d458de43ca300ee3fbf&UserName=waitingnow&Command=namecheap.domains.getList&ClientIp=27.18.218.10&PageSize=100&Page=1";
        //取出最近一百条数据
        $return = $this->geturl_namecheap($url);
        $returns = array();
        if($return){
            $return = $this->xmlToArray($return);
            $data = $return['CommandResponse']['DomainGetListResult']['Domain'];
        }
        if(!empty($data)){
            //更新到数据库中
            foreach ($data as $k=>$v){
                $params['updatetime'] = time();
                $params['api_type'] = 3;//namecheap
                $params['name'] = $v["@attributes"]['Name'];
                $dnslistname = Db::name('dns_list')->where('name',$params['name'])->find();
                if($dnslistname){
                    $returns = Db::name('dns_list')->where('name',$params['name'])->update($params);
                }else{
                    $params['createtime'] = time();
                    $returns = Db::name('dns_list')->insert($params, true);
                }
            }
        }
        return $returns;
    }

    public function geturl_namecheap($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    //XML转数组
    public function xmlToArray($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $values;
    }

    public function getEmailForwarding(){
        $url = "http://api.namecheap.com/xml.response?ApiUser=waitingnow&ApiKey=6e42d68999274d458de43ca300ee3fbf&UserName=waitingnow&Command=namecheap.domains.dns.getEmailForwarding&ClientIp=27.18.218.10&DomainName=thissavemylife.com";
        $return = $this->geturl_namecheap($url);
        $data = array();
        if($return){
            $return = $this->xmlToArray($return);
            dump($return);die;
        }
        return $data;
    }

    public function setEmailForwarding(){
        $url = "http://api.namecheap.com/xml.response?ApiUser=waitingnow&ApiKey=6e42d68999274d458de43ca300ee3fbf&UserName=waitingnow&Command=namecheap.domains.dns.setEmailForwarding&ClientIp=27.18.218.10&DomainName=thissavemylife.com&mailbox1=support&ForwardTo1=sneakercustservice@outlook.com";
        $return = $this->geturl_namecheap($url);
        $data = array();
        if($return){
            $return = $this->xmlToArray($return);
            dump($return);die;
        }
        return $data;
    }

    //转发邮箱
    public function setEmail(){

        $dnslistname = Db::name('dns_list')
            ->where('api_type','4')
            ->where('setEmailForwarding','0')
            ->order('setEmailtime asc')
            ->limit('100')
            ->select();
        $returns = 0;
        if(!empty($dnslistname)){
            //更新到数据库中
            foreach ($dnslistname as $k=>$v){
                $url1 = "http://api.namecheap.com/xml.response?ApiUser=waitingnow&ApiKey=6e42d68999274d458de43ca300ee3fbf&UserName=waitingnow&Command=namecheap.domains.dns.getEmailForwarding&ClientIp=27.18.218.10&DomainName=".$v['name'];
                $return1 = $this->geturl_namecheap($url1);
                if($return1){
                    $return1 = $this->xmlToArray($return1);
                    if($return1['@attributes']['Status'] == 'OK'){
                        if(isset($return1['CommandResponse']['DomainDNSGetEmailForwardingResult']["@attributes"]["Domain"]) && isset($return1['CommandResponse']['DomainDNSGetEmailForwardingResult']["Forward"]) && $return1['CommandResponse']['DomainDNSGetEmailForwardingResult']["Forward"] !== 'sneakercustservice@outlook.com'){
                            $this->error('已有转发邮箱：'.$return1['CommandResponse']['DomainDNSGetEmailForwardingResult']['Forward'].'，不需要设置');
                        }
                    }
                }else{
                    $this->error('获取不到邮箱地址');
                }

                $url = "http://api.namecheap.com/xml.response?ApiUser=waitingnow&ApiKey=6e42d68999274d458de43ca300ee3fbf&UserName=waitingnow&Command=namecheap.domains.dns.setEmailForwarding&ClientIp=27.18.218.10&DomainName=".$v['name']."&mailbox1=support&ForwardTo1=sneakercustservice@outlook.com";
                $return = $this->geturl_namecheap($url);
                if($return){
                    $return = $this->xmlToArray($return);
                    if($return['@attributes']['Status'] == 'OK'){
                        $params['setEmailForwarding'] = 1;
                    }
                }
                $params['setEmailtime'] = time();
                $returns = Db::name('dns_list')->where('id',$v['id'])->update($params);
            }
        }
        return $returns;
    }

    //修改dns记录
    public function setHosts(){
        $url = "http://api.namecheap.com/xml.response?ApiUser=waitingnow&ApiKey=6e42d68999274d458de43ca300ee3fbf&UserName=waitingnow&Command=namecheap.domains.dns.setHosts&ClientIp=27.18.218.10&SLD=domain51&TLD=com&HostName1=@&RecordType1=URL&Address1=http://www.namecheap.com&TTL1=100&HostName2=mynewcomp&RecordType2=A&Address2=12.56.67.78&TTL2=1000";
        $return = $this->geturl_namecheap($url);
        $data = array();
        if($return){
            $return = $this->xmlToArray($return);
            dump($return);die;
        }
        return $data;
    }
}
